package com.konne.nightmare.FastPublicOpinion.ui.information.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.SpanUtils;
import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpFragment;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpPresenter;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.AnnouncedTimeDialog;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.SheetDialog;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.DrawExamAct;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.ExamSettingAct;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;
import com.konne.nightmare.FastPublicOpinion.utils.ToastUtil;
import com.konne.nightmare.FastPublicOpinion.widget.QJCheckBox;
import com.konne.nightmare.FastPublicOpinion.widget.QJItemTitleView;
import com.konne.nightmare.FastPublicOpinion.widget.QJRadioGroup;

import butterknife.BindView;

/**
 * Create by ws.
 * on Data:2022/2/16  Time:下午 3:23.
 * description: 考试设置 .
 */
public class ExamSettingFrag extends BaseMvpFragment implements View.OnClickListener{
    @BindView(R.id.tv_s_num)
    TextView tv_s_num;
    private int sNum = 1;
    private int type;
    public static ExamSettingFrag getInstance(int type){
        ExamSettingFrag frag=new ExamSettingFrag();
        Bundle bundle=new Bundle();
        bundle.putInt("type",type);
        frag.setArguments(bundle);
        return frag;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type=getArguments().getInt("type",0);//0未开始
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return new BaseMvpPresenter();
    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return null;
    }

    @Override
    public void cancelLoadDialog() {

    }

    @Override
    public int getLayout() {
        return R.layout.frag_exam_setting;
    }

    @Override
    public void initView() {
        if(type==0){
            SpanUtils.with((TextView) findViewById(R.id.stv_note))
                    .append("当学生在当前设备上，每离开一次答题界面，将给予提示，如果切换页面超出上限后将被强制交卷。\n\n")
                    .append("注：设置次数时请考虑设备及网络等异常情况\n" +
                            "例如：手机端会存在电话呼入，电脑端广告弹框，不小心无操作\n" +
                            "切出等；\n" +
                            "特别注意：1.电脑端作答文件题时，上传图片算作1次切屏；\n" +
                            "2.手机端由于不同的操作系统，会导致切换到手机桌面，其他应\n" +
                            "用或者文件题上传图片时会每5秒记一次切屏。")
                    .setForegroundColor(0xff1C2B33).create();
        }else {
            SpanUtils.with((TextView) findViewById(R.id.stv_note)).append("目前考试正在进行中，无法进行切屏设置")
                    .setForegroundColor(0xff67788A).create();
        }

        SpanUtils.with((TextView) findViewById(R.id.stv_all_note))
                .append("建议学生使用")
                .append("火狐或谷歌浏览器")
                .setForegroundColor(0xff3D92EB)
                .append("参加考试（推荐火狐），且需要同意授权共享【整个屏幕】。否则将无法参加考试。")
                .create();
        QJCheckBox cb_cb = (QJCheckBox) findViewById(R.id.cb_cb);
        cb_cb.setOnCheckedChangeListener(new QJCheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(QJCheckBox var1, boolean var2) {
                findViewById(R.id.rg_auth).setVisibility(var2 ? View.VISIBLE : View.GONE);
            }
        });
        ((QJItemTitleView) findViewById(R.id.itv_toggle_screen)).setOnCheckedChangeListener(new QJItemTitleView.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(QJItemTitleView titleView, QJItemTitleView.Style style, boolean isChecked) {
                findViewById(R.id.ll_toggle_screen).setVisibility(isChecked ? View.VISIBLE : View.GONE);
            }
        });
        ((QJItemTitleView) findViewById(R.id.itv_online_invigilation)).setOnCheckedChangeListener(new QJItemTitleView.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(QJItemTitleView titleView, QJItemTitleView.Style style, boolean isChecked) {
                findViewById(R.id.ll_online_invigilation).setVisibility(isChecked ? View.VISIBLE : View.GONE);
            }
        });
        SuperTextView sv_title= (SuperTextView) findViewById(R.id.stv_title);
        sv_title.setDrawable(type==0?R.drawable.ic_exam_unstart:R.drawable.ic_exam_start);
        if(type==0){
            SpanUtils.with(sv_title)
                    .append("距离开始还剩 ")
                    .append("01:59:53")
                    .setForegroundColor(0xffF9485D)
                    .setFontSize(20,true).create();
        }else {
            sv_title.setText("注意：考试已结束，所有考试相关设置不能修改。");
        }
        findViewById(R.id.iv_sf_toast).setOnClickListener(this);
        findViewById(R.id.iv_sj_toast).setOnClickListener(this);
        findViewById(R.id.iv_qh_toast).setOnClickListener(this);
        findViewById(R.id.iv_jt_toast).setOnClickListener(this);
        initState();

    }
    private void initState(){
       if(type==0){
           findViewById(R.id.iv_s_sum).setOnClickListener(this);
           findViewById(R.id.iv_s_add).setOnClickListener(this);
           findViewById(R.id.itv_draw_exam).setOnClickListener(this);
           findViewById(R.id.itv_answer_announced).setOnClickListener(this);
           findViewById(R.id.itv_results_announced).setOnClickListener(this);
           findViewById(R.id.itv_multiple_selection).setOnClickListener(this);
           findViewById(R.id.itv_fill_blanks).setOnClickListener(this);
           findViewById(R.id.itv_sequence).setOnClickListener(this);
           findViewById(R.id.itv_option).setOnClickListener(this);
           findViewById(R.id.itv_answer_questions).setOnClickListener(this);
       }else {//可编辑
           findViewById(R.id.ll_s_s_num).setVisibility(View.GONE);
           ((QJItemTitleView) findViewById(R.id.itv_end_answer)).getQJSwitch().setEnabled(false);
           ((QJItemTitleView) findViewById(R.id.itv_case_sensitive)).getQJSwitch().setEnabled(false);
           ((QJItemTitleView) findViewById(R.id.itv_copy)).getQJSwitch().setEnabled(false);
           ((QJItemTitleView) findViewById(R.id.itv_paste)).getQJSwitch().setEnabled(false);
           ((QJItemTitleView) findViewById(R.id.itv_back)).getQJSwitch().setEnabled(false);
           ((QJItemTitleView) findViewById(R.id.itv_device)).getQJSwitch().setEnabled(false);
           ((QJItemTitleView) findViewById(R.id.itv_toggle_screen)).getQJSwitch().setEnabled(false);
           ((QJItemTitleView) findViewById(R.id.itv_online_invigilation)).getQJSwitch().setEnabled(false);
           ((QJCheckBox)findViewById(R.id.cb_cb)).setEnabled(false);
           ((QJCheckBox)findViewById(R.id.cb_sj)).setEnabled(false);
           ((QJCheckBox)findViewById(R.id.cb_qh)).setEnabled(false);
           ((QJCheckBox)findViewById(R.id.cb_jt)).setEnabled(false);
           QJRadioGroup radioGroup= (QJRadioGroup) findViewById(R.id.rg_auth);
           for (int i = 0; i < radioGroup.getRadioButtons().size(); i++) {
               View view= (View) radioGroup.getRadioButtons().get(i);
               view.setEnabled(false);
           }
       }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.itv_draw_exam:
                DrawExamAct.start();
                break;
            case R.id.iv_s_sum:
                sNum -= 1;
                if (sNum < 1) {
                    sNum = 1;
                }
                tv_s_num.setText(String.valueOf(sNum));
                break;
            case R.id.iv_s_add:
                sNum += 1;
                tv_s_num.setText(String.valueOf(sNum));
                break;
            case R.id.itv_fill_blanks:
                new SheetDialog(getContext())
                        .addSheet(new SheetDialog.Sheet("与答案不全匹配时，自动0分", 0, 0))
                        .addSheet(new SheetDialog.Sheet("与答案不全匹配时，手动批改", 0, 1))
                        .addSheet(new SheetDialog.Sheet("与答案不全匹配时，机器自动批改", 0, 2))
                        .setSheetListener(new SheetDialog.SheetListener() {
                            @Override
                            public void sheet(SheetDialog.Sheet sheet) {
                                QJItemTitleView itv = (QJItemTitleView) view;
                                TextView tv = (TextView) ((ViewGroup) itv.getContentView().getChildAt(0)).getChildAt(0);
                                tv.setText(sheet.txt);
                            }
                        }).show();
                break;
            case R.id.itv_multiple_selection:
                new SheetDialog(getContext())
                        .addSheet(new SheetDialog.Sheet("少选时，得一半分数", 0, 0))
                        .addSheet(new SheetDialog.Sheet("少选、多选、错选不得分", 0, 1))
                        .addSheet(new SheetDialog.Sheet("少选时，按照选项个数得分", 0, 2))
                        .setSheetListener(new SheetDialog.SheetListener() {
                            @Override
                            public void sheet(SheetDialog.Sheet sheet) {
                                QJItemTitleView itv = (QJItemTitleView) view;
                                TextView tv = (TextView) ((ViewGroup) itv.getContentView().getChildAt(0)).getChildAt(0);
                                tv.setText(sheet.txt);
                            }
                        }).show();
                break;
            case R.id.itv_results_announced:
                new SheetDialog(getContext())
                        .addSheet(new SheetDialog.Sheet("不公布", 0, 0))
                        .addSheet(new SheetDialog.Sheet("定时公布", 0, 1))
                        .addSheet(new SheetDialog.Sheet("学生交卷后公布", 0, 2))
                        .addSheet(new SheetDialog.Sheet("到截止日期公布", 0, 3))
                        .addSheet(new SheetDialog.Sheet("批阅后自动公布", 0, 4))
                        .setSheetListener(new SheetDialog.SheetListener() {
                            @Override
                            public void sheet(SheetDialog.Sheet sheet) {
                                QJItemTitleView itv = (QJItemTitleView) view;
                                TextView tv = (TextView) ((ViewGroup) itv.getContentView().getChildAt(0)).getChildAt(0);
                                tv.setText(sheet.txt);
                                if (sheet.action == 1) {
                                    new AnnouncedTimeDialog(getContext())
                                            .show();
                                }
                            }
                        }).show();
                break;
            case R.id.itv_answer_announced:
                new SheetDialog(getContext())
                        .addSheet(new SheetDialog.Sheet("不公布", 0, 0))
                        .addSheet(new SheetDialog.Sheet("定时公布", 0, 1))
                        .addSheet(new SheetDialog.Sheet("学生交卷后公布", 0, 2))
                        .addSheet(new SheetDialog.Sheet("到截止日期公布", 0, 3))
                        .setSheetListener(new SheetDialog.SheetListener() {
                            @Override
                            public void sheet(SheetDialog.Sheet sheet) {
                                QJItemTitleView itv = (QJItemTitleView) view;
                                TextView tv = (TextView) ((ViewGroup) itv.getContentView().getChildAt(0)).getChildAt(0);
                                tv.setText(sheet.txt);
                                if (sheet.action == 1) {
                                    new AnnouncedTimeDialog(getContext())
                                            .show();
                                }
                            }
                        }).show();
                break;
            case R.id.itv_sequence:
                new SheetDialog(getContext())
                        .addSheet(new SheetDialog.Sheet("不随机", 0, 0))
                        .addSheet(new SheetDialog.Sheet("全部题目顺序随机", 0, 1))
                        .addSheet(new SheetDialog.Sheet("同一题型顺序随机", 0, 2))
                        .setSheetListener(new SheetDialog.SheetListener() {
                            @Override
                            public void sheet(SheetDialog.Sheet sheet) {
                                QJItemTitleView itv = (QJItemTitleView) view;
                                TextView tv = (TextView) ((ViewGroup) itv.getContentView().getChildAt(0)).getChildAt(0);
                                tv.setText(sheet.txt);
                            }
                        }).show();
                break;
            case R.id.itv_option:
                new SheetDialog(getContext())
                        .addSheet(new SheetDialog.Sheet("不随机", 0, 0))
                        .addSheet(new SheetDialog.Sheet("随机", 0, 1))
                        .setSheetListener(new SheetDialog.SheetListener() {
                            @Override
                            public void sheet(SheetDialog.Sheet sheet) {
                                QJItemTitleView itv = (QJItemTitleView) view;
                                TextView tv = (TextView) ((ViewGroup) itv.getContentView().getChildAt(0)).getChildAt(0);
                                tv.setText(sheet.txt);
                            }
                        }).show();
                break;
            case R.id.itv_answer_questions:
                new SheetDialog(getContext())
                        .addSheet(new SheetDialog.Sheet("卡片式", 0, 0))
                        .addSheet(new SheetDialog.Sheet("一页式", 0, 1))
                        .setSheetListener(new SheetDialog.SheetListener() {
                            @Override
                            public void sheet(SheetDialog.Sheet sheet) {
                                QJItemTitleView itv = (QJItemTitleView) view;
                                TextView tv = (TextView) ((ViewGroup) itv.getContentView().getChildAt(0)).getChildAt(0);
                                tv.setText(sheet.txt);
                            }
                        }).show();
                break;
            case  R.id.iv_sf_toast:
                ToastUtil.showTitleToast("学生身份认证","考试前，学生需要上传身份证件照，并完成面部拍照，以便核实是否为本人参加考试。");
                break;
            case  R.id.iv_sj_toast:
                ToastUtil.showTitleToast("面部随机拍照","考试中，系统会对已开启摄像头的学生进行随机拍照，老师可在监考系统中查看。");
                break;
            case R.id.iv_qh_toast:
                ToastUtil.showTitleToast("页面切换检测","考试中，若学生在浏览器中切换标签页或在手机APP端切出、开多窗口等。行为都将被记录且系统自动标记考试异常");
                break;
            case R.id.iv_jt_toast:
                ToastUtil.showTitleToast("电脑桌面截屏","考试中，系统会对已授权共享桌面的学生进行截屏，老师可在监考系统中查看。");
                break;
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }
}
